package cn.com.css.taxInspection.attention.service.impl;

import cn.com.css.taxInspection.attention.entity.JcAyqksmEntity;
import cn.com.css.taxInspection.attention.mapper.JcAyqksmMapper;
import cn.com.css.taxInspection.attention.service.JcAyqksmService;
import cn.com.css.taxInspection.system.service.impl.HttpSessionService;
import cn.com.css.taxInspection.util.TsBqUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;

@Service
@Slf4j
public class JcAyqksmServiceImpl extends ServiceImpl<JcAyqksmMapper, JcAyqksmEntity> implements JcAyqksmService {

    @Resource
    private HttpSessionService httpSessionService;


    @Override
    public IPage<JcAyqksmEntity> queryJcAyqksmTable(JcAyqksmEntity jcAyqksmEntity) throws ParseException {
        String swjgDm = httpSessionService.getLogSwjgDm();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        IPage<JcAyqksmEntity> page = new Page<>(jcAyqksmEntity.getPage(), jcAyqksmEntity.getLimit());
        LambdaQueryWrapper<JcAyqksmEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(JcAyqksmEntity::getAjszjgDm, swjgDm);
        //案件编号
        if (TsBqUtils.isNotNull(jcAyqksmEntity.getJcajbh())) {
            queryWrapper.like(JcAyqksmEntity::getJcajbh, jcAyqksmEntity.getJcajbh());
        }
        //纳税人识别号
        if (TsBqUtils.isNotNull(jcAyqksmEntity.getNsrsbh())) {
            queryWrapper.like(JcAyqksmEntity::getNsrsbh, jcAyqksmEntity.getNsrsbh());
        }
        //纳税人名称
        if (TsBqUtils.isNotNull(jcAyqksmEntity.getNsrmc())) {
            queryWrapper.like(JcAyqksmEntity::getNsrmc, jcAyqksmEntity.getNsrmc());
        }
        //立案日期
        if (TsBqUtils.isNotNull(jcAyqksmEntity.getLa_startTime())) {
            queryWrapper.between(JcAyqksmEntity::getLarq, sdf.parse(jcAyqksmEntity.getLa_startTime()), sdf.parse(jcAyqksmEntity.getLa_endTime()));
        }

        // 根据标志位判断
        if ("ay".equals(jcAyqksmEntity.getAybz())) {
            if (TsBqUtils.isNotNull(jcAyqksmEntity.getBz())) {
                queryWrapper.eq(JcAyqksmEntity::getBz, jcAyqksmEntity.getBz());
            }
        }else if ("jc".equals(jcAyqksmEntity.getAybz())) {
            queryWrapper.eq(JcAyqksmEntity::getBz, "Y");
            if (TsBqUtils.isNotNull(jcAyqksmEntity.getBz())) {
                queryWrapper.eq(JcAyqksmEntity::getJcbz, jcAyqksmEntity.getBz());
            }
        }else if ("sl".equals(jcAyqksmEntity.getAybz())) {
            queryWrapper.eq(JcAyqksmEntity::getBz, "Y");
            if (TsBqUtils.isNotNull(jcAyqksmEntity.getBz())) {
                queryWrapper.eq(JcAyqksmEntity::getSlbz, jcAyqksmEntity.getBz());
            }
        }
        queryWrapper.orderByDesc(JcAyqksmEntity::getLarq);
        return page(page, queryWrapper);
    }
}
